package com.dataProcess;

import java.util.Date;

import com.enetiy.Balance;
import com.enetiy.Keeptime;
import com.enetiy.Mapspoor;
import com.enetiy.Ticket;
import com.enetiy.Ticketuser;
import com.enetiy.Users;
import com.enetiy.Vipmember;
import com.enetiy.Viprecord;
import com.service.DealService;
import com.service.MapService;
import com.service.TicketService;
import com.util.Log4gUtil;
import com.util.NetTimeUtil;
/**
 * 
 * @author hzy
 *
 */
public class Matter {
	/**
	 * 余额操作扣费和下网记录
	 * @param dealService 操作类
	 * @param userid 用户
	 * @param time 时长
	 * @return 成功或错误原因
	 * @throws Exception
	 */
	public int feeYue(DealService dealService,MapService mapService,int userid,int time,String NF_callid,String token,
			int keep_type,int keep_device,int areaid ,String netOrder ) throws Exception{
		Log4gUtil.Info("feeYue余额操作", userid+"", "计费时间", time+"分钟", NF_callid, token);
		int sign  = 0;
			//查询用户余额
			Balance bal = dealService.selectBalance(userid);
			if(bal!=null){//余额存在
				//查询用户预约计时
				Keeptime keep = dealService.selectKeeptime(userid, netOrder);
				if(keep!=null&&keep.getKeepTime()!=null){//预约计时存在
					int durationUtil = NetTimeUtil.getDurationUtil(new Date(), keep.getKeepTime());
						if(durationUtil>-40){
							keep.setKeepTime(null);//清空用户预约时间
							keep.setNetOrder("empty");
							keep.setPlanTime(0);
							dealService.updateKeeptime(keep);
							sign = 1;
							if(time>60*24){
								time = 60*24;
							}
							//根据计时扣费时长
							int i=0; //扣费时长
							if((float)time/60<1&&time%60<=11){
								i = 0;
							}else if((float)time/60<1&&time%60>11){
								i = 60;
							}else if((float)time/60>=1&&time%60<=3){
								i = (time/60)*60;
							}else{
								i = (1+time/60)*60;
							}
							
							//扣除时长 先扣购买时长,后扣赠送时长
							int a = bal.getLongTime();//购买时长
							int b = bal.getTicket04();//赠送时长
							if(a>=i||b==0){
								a=a-i;
							}else if((a+b)>=i){
								b=(a+b)-i;
								a=0;
							}else if((a+b)<i){
								a=(a+b)-i;
								b=0;
							}
							bal.setLongTime(a);
							bal.setTicket04(b);
							bal.setUpdateTime(new Date());
							Log4gUtil.Info("feeYue余额操作", userid+"", "余额时长", bal.getLongTime()+bal.getTicket04()+"分钟", NF_callid, token);
							dealService.updateBalance(bal);
						}
				}
			Mapspoor map = mapService.selectMapspoos(netOrder);
			if(map!=null){
				map.setOutnetTime(new Date());
				map.setOutnetMap(map.getGetnetMap());
				map.setPlanTime(time);
				map.setSurplusMin(bal.getLongTime()+bal.getTicket04());
				mapService.updateMapspoor(map);
			}
		}
		Log4gUtil.Info("feeYue操作", userid+"", "余额扣费标记", sign+"未扣费", NF_callid, token);
		return sign;
	}
	
	/**
	 * 新增vip
	 */
	public void saveVip(DealService dealService, Users user,Vipmember vip,TicketService ticketService) throws Exception{
		Vipmember  vipm = dealService.selVipmember(user.getId(),vip.getNameCode());
		//添加
			dealService.insetrVipmember(vip);
		Viprecord selViprecord = dealService.selViprecord(user.getId(), vip.getNameCode());
		if(selViprecord==null){//无记录则添加会员第一次赠送券
			Ticket ticket = ticketService.selectTicket("ticket-04");
			if(ticket!=null){
				Ticketuser t = new Ticketuser(user, ticket, new Date(), null, 0,ticket.getGiveTime());
				ticketService.insertTickeUser(t);
			}
		}
		//新增vip记录
		Viprecord v= new Viprecord(vip.getUsers().getId(),vip.getNameCode(),(double)vip.getPledge(),vip.getSetTime(),vip.getWayId(),"交付",vip.getAreaId());
		dealService.insetrViprecord(v);
	}
}
